const mongoose = require('mongoose');
const config = require('../config/config.json');

mongoose.connect(`mongodb://${config.host}:${config.port}/${config.dbname}`, { useNewUrlParser: true,useUnifiedTopology: true } );

//封装 db 函数
function db(success, error){
    mongoose.connection.on('open', () => {
        //执行 success 函数参数
        success();
    });

    //error 事件 当连接失败时会自动触发
    mongoose.connection.on('error', () => {
        error();
    });
}

//对外暴露 db 函数
module.exports = db;